#coding:utf-8
import requests
import threading
from lxml import etree

def get_page(start,end):
    urls=[]
    for i in range(start,end):
        url='https://www.21ks.net/lunwen/fuzhuangshejilunwen/List_'+str(i)+'.html'
        urls.append(url)
        for url in urls:
            res=requests.get(url).content.decode('gbk')
            tree=etree.HTML(res)
            p_list=tree.xpath('//*[@id="div08"]/div[3]')
            for p in p_list:
                title_list=p.xpath('./div/a/text()')
                url_list=p.xpath('./div/a/@href')
                # print(title)
                # print(url)
            for title,url in zip(title_list,url_list):
                # print(title)
                # print(url)
                try:
                    res=requests.get(url).content
                    tree=etree.HTML(res)
                    # print(title)
                    div_list=tree.xpath('//*[@id="div16"]/p//text()')
                    div_list=''.join(div_list)
                    # print(div_list)
                except UnicodeError:
                    continue
                with open('./文档1/'+title+'.doc','w',encoding='utf-8')as f:
                    # f.write(str(div_list).replace('\\r','\r').replace('\\n','\n').replace('\\t','\t'))
                    f.write(div_list)
def multiple_threads_test():#多线程
    page_ranges_lst=[
        (1,4),
        (5,8),
    ]
    th_lst=[]
    for page_range in page_ranges_lst:
        th=threading.Thread(target=get_page,args=(page_range[0],page_range[1]))
        th_lst.append(th)
        # print(th_lst)
        for th in th_lst:
            th.start()
    for th in th_lst:
        th.join()
    print('xiazai')

if __name__ == '__main__':

    mt=multiple_threads_test()



# url='https://www.21ks.net/lunwen/fuzhuangshejilunwen/162152.html'
# res=requests.get(url).content
# tree=etree.HTML(res)
# div_list=tree.xpath('//*[@id="div16"]/p//text()')
# print(str(div_list).replace('\\r','\r').replace('\\n','\n').replace('\\t','\t'))